<?php
    class Model_Friendship extends Joob_Db_Relation {
	    public static $to_class = 'Model_Friend';
	    public static $from_class = 'Model_User';
	
		function __construct() {
			parent::__construct(self::$from_class,self::$to_class,'user_id1');
		}
		
		public function getFriends($user_id,$page,$page_size){
			$this->limit($page*$page_size.','.$page_size);
			$this->appendJoin('or','user_id2');
			$this->condition(' and users.id<>'.$user_id.' and (user_id1='.$user_id.' or user_id2='.$user_id.') and state=1 ');
			$this->select(' users.id as user_id, fullname, lat, lng, avatar, gender');
			return $this->get();
		}
		public function getFriendsNearby($user_id,$maxLat,$minLat,$maxLng,$minLng, $page,$page_size){
			$this->limit($page*$page_size.','.$page_size);
			$this->appendJoin('or','user_id2');
			
//					$condition = "friend_id={$this->id} and state=1 and (lat < {$maxLat}) and (lat > {$minLat})
//            and (lng < {$maxLng}) and (lng > {$minLng})";
			$condition = ' and users.id<>'.$user_id.' and (user_id1='.$user_id.' or user_id2='.$user_id.') and state=1  and (lat < '.$maxLat.') and (lat > '.$minLat.')
	          and (lng < '.$maxLng.') and (lng > '.$minLng .')';
			$this->condition($condition);
			$this->select(' users.id as user_id, fullname, lat, lng, avatar, gender');
			return $this->get();
		}
    }
?>